package cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.service.banner;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner.vo.BannerCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner.vo.BannerExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner.vo.BannerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner.vo.BannerUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.convert.banner.BannerConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.dal.mysql.banner.BannerMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 小程序-banner Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper;

    @Override
    public Integer createBanner(BannerDO banner) {
        if (banner.getType()==2){
            if (banner.getTid()==null)
                throw new ServiceException(500,"请填写轮盘信息");
        }
        // 插入
        bannerMapper.insert(banner);
        // 返回
        return banner.getId();
    }

    @Override
    public void updateBanner(BannerDO updateReqVO) {
        if (updateReqVO.getType()==2){
            if (updateReqVO.getTid()==null)
                throw new ServiceException(500,"请填写轮盘信息");
        }
        // 校验存在
        validateBannerExists(updateReqVO.getId());
        // 更新
        bannerMapper.updateById(updateReqVO);

    }

    @Override
    public void deleteBanner(Integer id) {
        // 校验存在
        validateBannerExists(id);
        // 删除
        bannerMapper.deleteById(id);
    }

    private void validateBannerExists(Integer id) {
        if (bannerMapper.selectById(id) == null) {
            throw exception(500);
        }

    }

    @Override
    public BannerDO getBanner(Integer id) {
        return bannerMapper.selectById(id);
    }

    @Override
    public List<BannerDO> getBannerList(Collection<Integer> ids) {
        return bannerMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BannerDO> getBannerPage(BannerPageReqVO pageReqVO) {
        return bannerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BannerDO> getBannerList(BannerExportReqVO exportReqVO) {
        return bannerMapper.selectList(exportReqVO);
    }

}
